package com.itheima.controller;

import com.itheima.domain.PmsProductAttributeCategory;
import com.itheima.manager.PmsProductAttributeCategoryManager;
import com.itheima.util.R;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

@RestController
public class PmsProductAttributeCategoryController {

    @Autowired
    PmsProductAttributeCategoryManager pmsProductAttributeCategoryManager;


    /**
     * 添加商品属性分类
     * @param name
     * @return
     */
    @PostMapping("/productAttribute/category/create")
    public R addProductAttributeCategory(@RequestParam String name){
        return pmsProductAttributeCategoryManager.addProductAttributeCategory(name);
    }

    /**
     * 删除单个商品属性分类
     * @param id
     * @return
     */
    @GetMapping ("/productAttribute/category/delete/{id}")
    public R deleteOne(@PathVariable Long id){
        return pmsProductAttributeCategoryManager.deleteOne(id);
    }

    /**
     * 分页获取所有商品属性分类
     * @param pageNum
     * @param pageSize
     * @return
     */
    @GetMapping("/productAttribute/category/list")
    public R findByPage(@RequestParam(defaultValue = "1") Integer pageNum,
                        @RequestParam(defaultValue = "10") Integer pageSize){
        return pmsProductAttributeCategoryManager.findByPage(pageNum,pageSize);
    }


    /**
     * 获取所有商品属性分类及其下属性
     * @return
     */
    @GetMapping("/productAttribute/category/list/withAttr")
    public R findWithAttr(){
        return pmsProductAttributeCategoryManager.findWithAttr();
    }

    /**
     * 修改商品属性分类
     * @param id
     * @param name
     * @return
     */
    @PostMapping("/productAttribute/category/update/{id}")
    public R updateCategory(@PathVariable Long id,@RequestParam String name){
        PmsProductAttributeCategory pmsProductAttributeCategory = new PmsProductAttributeCategory();
        pmsProductAttributeCategory.setId(id);
        pmsProductAttributeCategory.setName(name);
        return pmsProductAttributeCategoryManager.updateCategory(id,name);
    }


    /**
     *获取单个商品属性分类信息
     * @param id
     * @return
     */
    @GetMapping("/productAttribute/category/{id}")
    public R getOneById(@PathVariable Long id){
        return pmsProductAttributeCategoryManager.getOneById(id);
    }
}
